package com.showde.shoppingmall.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ActivityGroup;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.showde.shoppingmall.R;
import com.showde.shoppingmall.ShoppingMallApplication;
import com.showde.shoppingmall.adapter.AddressItemAdapter;
import com.showde.shoppingmall.business.logic.AddressLogic;
import com.showde.shoppingmall.business.logic.CallbackData;
import com.showde.shoppingmall.business.logic.UICallbackInterface;
import com.showde.shoppingmall.db.AddressDBCommand;
import com.showde.shoppingmall.domain.Address;
import com.showde.shoppingmall.utils.ActivityDispatch;

@SuppressWarnings("deprecation")
public class AddressManagerActivity extends BaseActivity {

	private static final String TAG = "AddressManagerActivity";

	private static final int MSG_FETCHADDRESS_SUCCESS = 0x10001;// 获取地址信息成功
	private static final int MSG_FETCHADDRESS_FAIL = 0x10002; // 获取地址信息失败

	private List<Address> addresses;

	private ListView addressListView;

	private AddressItemAdapter itemAdapter;

	private TextView addAddress;

	private AddressDBCommand addressDBCommand;

	private Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_FETCHADDRESS_SUCCESS:
				refreshList();
				break;
			case MSG_FETCHADDRESS_FAIL:
				break;
			}
			super.handleMessage(msg);
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onResume() {
		super.onResume();
		System.out.println("onResume");
		refreshList();
		if (!ShoppingMallApplication.userSession.isSessionAvailable()) {
			// ToastUtils.showToast(this, "请登录吉鸿商城...");
			ActivityDispatch.openNewActivity(this, LoginActivity.class,
					ActivityDispatch.getActivityGroup(groupTag),
					"AddressManagerActivity", groupTag);
			return;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.img_add:
			ActivityDispatch.openNewActivity(AddressManagerActivity.this,
					AddressEditActivity.class,
					ActivityDispatch.getActivityGroup(groupTag),
					"ProductDetailsActivity", groupTag);
			break;
		}
	}

	@Override
	protected void initParam() {
		ActivityGroup activityGroup = ActivityDispatch
				.getActivityGroup(groupTag);
		if (activityGroup == null) {
			groupTag = ActivityDispatch.GROUP_MORE;
		}

		if (addressDBCommand == null) {
			addressDBCommand = new AddressDBCommand(this);
		}

		AddressLogic.fetchAddress(this, new UICallbackInterface() {

			@Override
			public void onSuccess(CallbackData data) {
				handler.sendEmptyMessage(MSG_FETCHADDRESS_SUCCESS);
			}

			@Override
			public void onError(String errorMsg) {
				Log.d(TAG, "fetchAddress:" + errorMsg);
			}
		});
	}

	@Override
	protected void initView() {
		setContentView(R.layout.activity_addressmanager);
		addressListView = (ListView) findViewById(R.id.lst_address);
		addAddress = (TextView) findViewById(R.id.img_add);
		addAddress.setOnClickListener(this);
	}

	private void refreshList() {

		addresses = addressDBCommand.queryAll();

		if (addresses == null) {
			addresses = new ArrayList<Address>();
		}

		itemAdapter = (AddressItemAdapter) addressListView.getAdapter();
		if (itemAdapter == null) {
			itemAdapter = new AddressItemAdapter(this, addresses);
			addressListView.setAdapter(itemAdapter);
		} else {
			itemAdapter.resetData(addresses);
		}
		itemAdapter.notifyDataSetChanged();
	}

}
